<?php
    /**
     * Defines the configuration of Mysql.
     */
    class MysqlConfig
    {
        /**
         * Creates a MysqlConfig
         * @param String $file The customize file path for mysql connection.
         */
        public function MysqlConfig( $file = null )
        {
            $this->file = null ;
            
            // Tests if the custom file exists
            if( file_exists( $file ) )
            {
                $this->file = $file ;
            }
            else
            {
                // Tests if the mysql config file exists.
                if( file_exists( self::MYSQL_CONFIG ) )
                {
                    $this->file = self::MYSQL_CONFIG ;
                }
                else
                {
                    if( file_exists( self::DEFAULT_MYSQL_CONFIG ) )
                    {
                        $this->file = self::DEFAULT_MYSQL_CONFIG ;
                    }
                }
            } 
            
            if( $this->file !== null )
            {
                try
                {
                    $handle = fopen( $this->file , "r" ) ;
                    while( !feof( $handle ) )
                    {
                        $page        = fgets( $handle , 4096 ) ;
                        $page        = explode( '=' , trim( $page ) ) ;
                        $prop        = trim( $page[0] ) ;
                        $value       = trim( $page[1] ) ;
                        $this->$prop = $value ;
                    }
                    fclose( $handle ) ;
                }
                catch( Exception $e )
                {
                    throw  new Exception( "The mysql config file doesn't exist!" ) ;
                }
            }
            else
            {
                throw new Exception( "The mysql config file doesn't exist!" ) ;
            }
        }
        
        /**
         * The default path of default_mysql.properties file.
         * @var String
         */
        const DEFAULT_MYSQL_CONFIG = "../config/default_mysql.properties" ;
        
        /**
         * The default path of mysql.properties.
         * @var String
         */
        const MYSQL_CONFIG = "../config/mysql.properties" ;
        
        /**
         * The name of the config file.
         * @var String
         */
        public $file ;
        
        /**
         * The db host.
         * @var String
         */ 
        public $host ;
            
        /**
         * The db name.
         * @var String
         */
        public $name ;
        
        /**
         * The db user password.
         * @var String
         */ 
        public $pass ;
        
        /**
         * The db user name.
         * @var String
         */ 
        public $user ;
    }
?>